﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using Microsoft.Web.WebSockets;
using Channels;
using Cards;
using Games;

namespace WebSocketDemo
{
    public class TestWebSocketHandler : WebSocketHandler
    {
        private static WebSocketCollection clients = new WebSocketCollection();
        private string strUsername;
        private string strPassword;
        
        public override void OnOpen()
        {
            this.strUsername = this.WebSocketContext.QueryString["Username"];
            this.strPassword = this.WebSocketContext.QueryString["Password"];
            clients.Add(this);
            clients.Broadcast(strUsername + " has connected.");
            //this.Send("Channels: " + Channel.LstChannels.ToString());
        }

        public override void OnMessage(string message)
        {
            /*if (message.IndexOf("Channel") == 0)
                if (Channels.Channel.joinChannel(Int32.Parse(message.Substring(8, message.Length))) == true)
                    clients.Broadcast(this.strUsername + " has joined channel " + message.Substring(8, message.Length));
                else
                    clients.Broadcast("The channel " + message.Substring(8, message.Length) + " is fulled");
            if (message.IndexOf("Game") == 0)
                clients.Broadcast(this.strUsername + " has joined game " + message.Substring(5, message.Length));*/
            //if (message.IndexOf("Distribute") == 0)
                //clients.Broadcast(string.Format("{0} has been distributed: {1}", name, ManageCards.DistributeCards(ga.).INumber + " " + ManageCards.arrayCard[i].UPCard));
            /*
            GameTypes objGT = new GameTypes();
            objGT = GameTypes.BaCay;
            if (ManageCards.arrayCard == null)
                ManageCards.createCards(objGT);
            Card ObjCard1 = ManageCards.DistributeCards(objGT);
            Card ObjCard2 = ManageCards.DistributeCards(objGT);
            Card ObjCard3 = ManageCards.DistributeCards(objGT);

            if (ObjCard1 != null)
                clients.Broadcast(string.Format("{0} has been distributed: {1}", strUsername, ObjCard1.INumber + " " + ObjCard1.UPCard));
            if (ObjCard2 != null)
                clients.Broadcast(string.Format("{0} has been distributed: {1}", strUsername, ObjCard2.INumber + " " + ObjCard2.UPCard));
            if (ObjCard3 != null)
                clients.Broadcast(string.Format("{0} has been distributed: {1}", strUsername, ObjCard3.INumber + " " + ObjCard3.UPCard));
             * */
        }
        
        public override void OnClose()
        {
            clients.Remove(this);
            clients.Broadcast(string.Format("{0} has gone away.", strUsername));
        }
    }
}